﻿using System;
using System.Collections;
using System.Web;
using Devv.Core.Utils;

namespace BikeBouncer.Site
{
	public class SecurityManager
	{
		#region Properties

		private static Hashtable SearchIPCache = new Hashtable();
		private static DateTime SearchIPDate = DateTime.MinValue;

		#endregion

		#region Search

		public static void SearchCheckIP()
		{
			Int32 count = 0;
			String ip = LocationManager.RequestIP;

			if (String.IsNullOrEmpty(ip))
			{
				ip = "0.0.0.0";
			}

			if (SearchIPCache.ContainsKey(ip))
			{
				count = (Int32)SearchIPCache[ip];
			}

			if (SearchIPDate.AddMinutes(ConfigUtil.GetConfig("BikeBouncer.Search.TimeLimitIP", 60)) < DateTime.Now)
			{
				count = 0;
				SearchIPDate = DateTime.Now;
			}
			else
			{
				count++;
			}
			
			if (count > ConfigUtil.GetConfig("BikeBouncer.Search.MaxCountIP", 200))
			{
				DenyAccess("IP " + ip + " search count: ");
			}

			SearchIPCache[ip] = count;
		}

		#endregion

		#region Deny

		private static void DenyAccess(String reason)
		{
			Log(reason);

			HttpContext.Current.Response.StatusCode = 403;
			HttpContext.Current.Response.StatusDescription = "Forbidden";
		}

		#endregion

		#region Log

		private static void Log(String text)
		{
			
		}

		#endregion
	}
}